home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / library / queue.lzh / queue / queue_test.c < prev    next >
C/C++ Source or Header  |  1995-10-15  |  3KB  |  138 lines

  1.  
  2. #include <exec/exec.h>
  3. #include <proto/exec.h>
  4. #include <proto/dos.h>
  5.  
  6. #include "queue.h"
  7. #include "queue_inline.h"
  8.  
  9. struct ExecBase *SysBase = NULL;
  10. struct DosLibrary *DOSBase = NULL;
  11. struct Library *QueueBase = NULL;
  12.  
  13. typedef struct {
  14.   QMessage qmsg;
  15.   char     contents;
  16. } MyQMessage;
  17.  
  18. Block (int *abort)
  19. {
  20.   int r;
  21.  
  22.   if (r = rand () % 3)
  23.     Delay (r * 5);
  24.   if (SetSignal (0, SIGBREAKF_CTRL_F) & SIGBREAKF_CTRL_F)
  25.     *abort = 1;
  26. }
  27.  
  28. int
  29. main (int argc, char *argv[])
  30. {
  31.   ULONG sigbit, sigmask;
  32.   MyQMessage qmsg, *msg;
  33.   QHandle qh;
  34.   char *queuename = "testqueue";
  35.   char contents = 0;
  36.   int r, abort = 0;
  37.  
  38.   if (argc == 2)
  39.   {
  40.     contents = argv[1][0];
  41.   }
  42.   SysBase = *(struct ExecBase **)4;
  43.   if (!(DOSBase = (struct DosLibrary *) OpenLibrary ("dos.library", 37)))
  44.     return 1;
  45.  
  46.   if ((sigbit = AllocSignal (-1)) == -1)
  47.   {
  48.     printf ("Failed to allocate signal.\n");
  49.     return 1;
  50.   }
  51.   sigmask = 1 << sigbit;
  52.   msg = &qmsg;
  53.  
  54.   if (!(QueueBase = OpenLibrary ("queue.library", 0)))
  55.   {
  56.     printf ("Failed to open queue.library.\n");
  57.     return 1;
  58.   }
  59.   if (!(qh = QOpen (queuename, contents ? QMODE_SEND : QMODE_LISTEN, sigbit)))
  60.   {
  61.     printf ("Failed to open \"%s\" queue.\n", queuename);
  62.     return 1;
  63.   }
  64.   if (!contents) /* Client */
  65.   {
  66.     while (1)
  67.     {
  68.       if (abort || Wait (sigmask | SIGBREAKF_CTRL_F) & SIGBREAKF_CTRL_F)
  69.       {
  70.     Forbid ();
  71.         while (msg = (MyQMessage *) QGetMsg (qh));
  72.     QClose (qh);
  73.     Permit ();
  74.         printf ("Exiting.\n");
  75.     return 0;
  76.       }
  77.       printf ("Signal.\n");
  78.  
  79.       if (rand () % 2) /*** Process all messages ***/
  80.       {
  81.         while (msg = (MyQMessage *) QGetMsg (qh))
  82.       printf ("Got message %c.\n", msg -> contents);
  83.       }
  84.       else /*** Reply message & block ***/
  85.       {
  86.         if (msg = (MyQMessage *) QGetMsg (qh))
  87.       printf ("Got message %c (replying,blocking).\n", msg -> contents);
  88.  
  89.         if (r = rand () % 2)
  90.         {
  91.       QReplyMsg (qh);
  92.           Block (&abort);
  93.         }
  94.         else
  95.         {
  96.           Block (&abort);
  97.       QReplyMsg (qh);
  98.         }
  99.       }
  100.     }
  101.   }
  102.   else
  103.   {
  104.     msg -> contents = contents;
  105.  
  106.     while (1)
  107.     {
  108.       if (!abort)
  109.       {
  110.         if (msg -> contents < '9')
  111.         {
  112.       msg -> contents ++;
  113.       printf ("Next message: %c.\n", msg -> contents);
  114.         }
  115.         QAddMsg (qh, (QMessage *) msg);
  116.       }
  117.       if (abort || Wait (sigmask | SIGBREAKF_CTRL_F) & SIGBREAKF_CTRL_F)
  118.       {
  119.         while ((r = QClose (qh)) > 0)
  120.         {
  121.           printf ("Cannot exit: %d messages left in queue.\n", r);
  122.       Delay (10);
  123.           QGetMsg (qh);
  124.         }
  125.         printf ("All done.\n");
  126.         return 0;
  127.       }
  128.       printf ("Signal.\n");
  129.       if (QGetMsg (qh) == (QMessage *) msg)
  130.         printf ("Got my message (%c).\n", msg -> contents);
  131.       else
  132.         printf ("Got wrong message (%c).\n", msg -> contents);
  133.  
  134.       Block (&abort);
  135.     }
  136.   }
  137. }
  138.